home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Utilities / pLayer 1.0d5 / pLayer Source / pLayer.p < prev    next >
Encoding:
Text File  |  1993-06-09  |  7.0 KB  |  265 lines  |  [TEXT/MPS ]

  1. {
  2.     $Workfile:   pLayer.p  $
  3.     $Revision:   1.0  $
  4.  
  5.     A hack that uses System 7.1's Text Services Manager to display a floating pallette of scripts,
  6.     QuicKeys, and more.
  7.     
  8.     This file is just a very simple application shell.  All of the interesting stuff is in
  9.     pLayerUnit.p.
  10.  
  11.     © 1993 CE Software, Inc.  All rights reserved.
  12.  
  13.     WHEN    WHO        WHAT
  14.  
  15. •••••
  16.     
  17. •••••
  18. }
  19.  
  20. PROGRAM pLayer;
  21.  
  22. USES Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, MacPrint, AppleTalk, AppleEvents,
  23.     GestaltEqu,Folders,Aliases,pLayerUnit; {$R-} {$D+}
  24.  
  25. CONST
  26.     appleMenu = 1;
  27.     MyFileMenu = 2;
  28.     
  29.     lastMenu =    MyFileMenu;              { number of menus w/ debug}
  30.       
  31. VAR    myMenus:    ARRAY [appleMenu..lastMenu] OF MenuHandle; {Handles to all of the menus}
  32.     theChar:    CHAR;                    {keyboard input goes here}
  33.     doneFlag:    BOOLEAN;                {set when the user quits the program}
  34.     myEvent:    EventRecord;            {returned by GetNextEvent}
  35.     watchHdl:    CursHandle;                {the wait cursor}
  36.     IsForeGround:    Boolean;            {Are we a foreground app?}
  37.     
  38. Procedure SetIsForeGround;
  39. type ip=^integer; ih=^ip;
  40. var h:ih;
  41. begin
  42.     IsForeGround:=true;
  43.     h:=pointer(GetResource('SIZE',-1));
  44.     if h<>nil then
  45.         if bitand(h^^,$0400)<>0 then IsForeGround:=false;
  46.     end;
  47.  
  48. {--------------------------------------------------------------------------
  49.     
  50.     Apple Event handlers
  51.         Just handle the required commands.  We don't print, so just open files, open the
  52.         app, and quit.
  53.  
  54.  --------------------------------------------------------------------------}
  55. FUNCTION GotRequiredParams( theAppleEvent: AppleEvent ): OSErr ;        { <aevt> }
  56. VAR    typeCode: DescType ;
  57.     actualSize: Size ;
  58.     err: OSErr ;
  59. BEGIN
  60.     err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
  61.                     typeWildCard, typeCode, nil, 0, actualSize) ;    { nil ok: need only function result }
  62.     IF err = errAEDescNotFound THEN GotRequiredParams := noErr
  63.         ELSE IF err = noErr THEN GotRequiredParams := errAEEventNotHandled
  64.             ELSE GotRequiredParams := err ;
  65.     END ; { GotRequiredParams }
  66.  
  67.  
  68. Function  HandleAEQuit(theAppleEvent,reply:AppleEvent; refcon:longint):OSErr;
  69. begin
  70.     HandleAEQuit:=GotRequiredParams( theAppleEvent );
  71.     doneflag:=true;
  72.     end;
  73.  
  74. Function  HandleAEOpenApp(theAppleEvent,reply:AppleEvent; refcon:longint):OSErr;
  75. begin
  76.     HandleAEOpenApp:=GotRequiredParams( theAppleEvent );
  77.     DoOpenApp(IsForeGround);
  78.     end;
  79.  
  80. Function  HandleAEOpenFile(theAppleEvent,reply:AppleEvent; theblock:ptr):OSErr;
  81. var StuffToDo,EachFile:AEDesc;
  82.     returnedtype:AEKeyword;
  83.     FileSpec:FSSpec;
  84.     waschanged:boolean;
  85.     
  86.     Procedure FailErr(io:OSErr);
  87.     begin
  88.       if io<>NoErr then begin
  89.             HandleAEOpenFile:=io;
  90.             exit(HandleAEOpenFile);
  91.             end;
  92.       end;
  93. begin
  94.     FailErr(AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,StuffToDo));
  95.     FailErr( GotRequiredParams( theAppleEvent ) ) ;
  96.     
  97.     FailErr(AEGetNthDesc(StuffToDo,1,typeAlias,returnedtype,EachFile));
  98.     FailErr(ResolveAlias(nil,pointer(EachFile.datahandle),FileSpec,waschanged));
  99.     DoOpenFile(FileSpec);
  100.  
  101.     HandleAEOpenFile:=NoErr;
  102.     end;
  103.  
  104. PROCEDURE InstallAppleEvents ;
  105. VAR
  106.     aevtErr: OSErr ; theproc:procptr;
  107. BEGIN
  108.     aevtErr := AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,@HandleAEQuit, 0, false ) ;
  109.     aevtErr := AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,@HandleAEOpenApp, 0, false ) ;
  110.     aevtErr := AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,@HandleAEOpenFile, 0, false ) ;
  111.     END ;
  112.  
  113. {--------------------------------------------------------------------------
  114.     
  115.     Setup
  116.         Initialize our managers, Install our AppleEvent handlers, and then init the pallette code.
  117.  
  118.  --------------------------------------------------------------------------}
  119. PROCEDURE SetUp;
  120.  
  121. VAR i:integer;
  122.     numfiles:integer;
  123.     templong:longint;
  124.     io:OSErr;
  125.     p:procptr;
  126.     
  127.     Procedure DoNotRun(io:OSErr; s:str255);
  128.     begin
  129.       if io<>NoErr then begin
  130.         if IsForeGround then begin
  131.             paramtext(s,'','','');
  132.             i:=StopAlert(801,nil);
  133.             end
  134.             else debugstr(s);
  135.         ExitToShell;
  136.         end;
  137.       end;
  138.       
  139.  
  140. BEGIN
  141.  
  142.     SetIsForeGround;
  143.     
  144.     if IsForeGround then begin
  145.         MaxApplZone;
  146.         InitGraf(@thePort);         {I need QuickDraw}
  147.         InitFonts;
  148.         InitWindows;
  149.         TEInit;
  150.         InitDialogs(NIL);
  151.         
  152.         watchHdl := GetCursor(4);
  153.         HNoPurge (Pointer(watchHdl));
  154.         
  155.         InitMenus;
  156.         myMenus[appleMenu] := GetMenu(appleMenu);
  157.         AddResMenu(myMenus[1],'DRVR');    { desk accessories }
  158.         myMenus[MyFileMenu] := GetMenu(MyFileMenu);
  159.         ClearmenuBar;
  160.         InsertMenu(myMenus[applemenu],0);
  161.         InsertMenu(myMenus[MyFileMenu],0);
  162.         DrawMenuBar;
  163.         
  164.         end
  165.         else begin
  166.             SetApplLimit(pointer(ord4(GetApplLimit)-32768)); {give an additional 32K to stack}
  167.             MaxApplZone;
  168.             InitGraf(@thePort);         {still need QuickDraw}
  169.             end;
  170.     
  171.     doneFlag := FALSE;
  172.     
  173.     {See if we can do AppleEvents}
  174.     io:=Gestalt(gestaltAppleEventsAttr,templong);
  175.     if io=NoErr then if not odd(templong) then io:=-1;
  176.     DoNotRun(io,'Apple Events not installed');
  177.     
  178.     InstallAppleEvents;
  179.     
  180.     DoNotRun(InitLayer,'Unable to initialize our Layer stuff');
  181.     
  182.     END;    { of SetUp}
  183.  
  184. {--------------------------------------------------------------------------
  185.     
  186.     DoCommand
  187.         Handle our File and Apple menus.
  188.  
  189.  --------------------------------------------------------------------------}
  190. PROCEDURE DoCommand (commandkey: boolean);
  191. VAR name: str255;
  192.     DeskAccUp : boolean;
  193.     num, theMenu, theItem: integer;
  194.     mresult: longint;
  195. BEGIN {This handles the actions that are initiated through the Menu Manager}
  196.     If Commandkey then mResult := MenuKey(theChar)
  197.         else mResult := MenuSelect (myEvent.where);
  198.     theMenu := HiWord(mResult); theItem := LoWord(mResult);
  199.     CASE theMenu OF
  200.         appleMenu: begin
  201.             IF theItem = 1 THEN num:=Alert(800,nil)
  202.                 ELSE BEGIN
  203.                     GetItem(myMenus[appleMenu],theItem,name);
  204.                     num := OpenDeskAcc(name);
  205.                     END
  206.             END;
  207.         MyFileMenu:
  208.             case TheItem of
  209.                 1:HandleNew;
  210.                 2:HandleOpen;
  211.                 3:HandleSave;
  212.                 5:doneFlag := TRUE; { Quit }
  213.                 end;
  214.         END;    { of menu case }
  215.     HiliteMenu(0)
  216.     END;  { of DoCommand }
  217.  
  218. {--------------------------------------------------------------------------
  219.     
  220.     MainEventLoop
  221.         Handle our events.  We don't do very much, just handle menus, so there's
  222.         not much here.
  223.         
  224.         We also call the pallette code's idle routine here.
  225.  
  226.  --------------------------------------------------------------------------}
  227. PROCEDURE MainEventLoop;
  228. var dummy:boolean;
  229.     tempwindow:windowptr;
  230.     io:OSErr;
  231. BEGIN
  232.     REPEAT
  233.         SetCursor(arrow);
  234.         
  235.         dummy := WaitNextEvent(everyEvent,myEvent,$7FFFFFFF,nil);
  236.         
  237.         CASE myEvent.what OF
  238.             kHighLevelEvent:
  239.                 io:=AEProcessAppleEvent( myevent ) ;                { <aevt> }
  240.             mouseDown:
  241.                 if IsForeGround then
  242.                     CASE FindWindow(myEvent.where,tempWindow) OF
  243.                         inSysWindow: begin SystemClick(myEvent,tempWindow); end;
  244.                         inMenuBar: DoCommand(FALSE);
  245.                         END;
  246.             
  247.             keyDown, autoKey:
  248.                 if IsForeGround then begin
  249.                     theChar := CHR(myEvent.message MOD 256);    { Mac characters use 8 bits }
  250.                     IF BitAnd(myEvent.modifiers,CmdKey) <> 0 THEN begin
  251.                         if dummy then DoCommand(TRUE);
  252.                         end;
  253.                     END;    { of keyDown }
  254.             END;    { of event case }
  255.         LayerIdleProc(doneflag);
  256.         UNTIL doneFlag;
  257.     END;
  258.  
  259. BEGIN    { main program }
  260.   SetUp;
  261.   MainEventLoop;
  262.   if IsForeGround then SetCursor (watchHdl^^);
  263.   CloseLayer;
  264.   END.
  265.